package com.hmyd.hmyd_android.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.MailVerifyCode;
import com.hmyd.hmyd_android.dto.VerifyCode;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.RegPatterns;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 修改邮箱
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_mail_update)
public class MailUpdateActivity extends BasicActivity {

	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.email)
	private EditText email;
	@ViewInject(R.id.et_code)
	private EditText et_code;
	@ViewInject(R.id.btn_get_code)
	private Button btn_get_code;
	@ViewInject(R.id.save)
	private Button save;

	private String mCodeId;

	private final int SEND_REGISTER_VERIFY_CODE = 0;
	private final int SUBMIT = 1;

	@OnClick({ R.id.btn_get_code, R.id.save })
	private void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_get_code:
			onRequest(SEND_REGISTER_VERIFY_CODE, UrlConstant.SENDSAFECENTERVERIFYCODE,
					HttpMethod.GET, null);
			break;
		case R.id.save:
			RequestParams params2 = new RequestParams();
			params2.addBodyParameter("mobileSessionId", mCodeId);
			params2.addBodyParameter("mobileCode", et_code.getText().toString().trim());
			params2.addBodyParameter("email", email.getText().toString().trim());

			onRequest(SUBMIT, UrlConstant.DOUPDATEEMAILCONFIRMSUBMIT, HttpMethod.POST, params2);

			break;

		default:
			break;
		}

	}

	@Override
	public void onPostSuccess(int postId, String result) {

		Gson gson = new Gson();
		switch (postId) {
		case SEND_REGISTER_VERIFY_CODE:
			handler.postDelayed(runnable, 0);
			java.lang.reflect.Type type = new TypeToken<VerifyCode>() {
			}.getType();
			VerifyCode verifyCode = gson.fromJson(result, type);
			mCodeId = verifyCode.codeId;
			break;
		case SUBMIT:
			java.lang.reflect.Type type2 = new TypeToken<MailVerifyCode>() {
			}.getType();
			MailVerifyCode verifyCode2 = gson.fromJson(result, type2);
			MyToast.showHMToast(this, true, verifyCode2.info);
			finish();
			break;
		default:
			break;

		}

	}

	private int second = 60;
	Handler handler = new Handler();
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if (second >= 0) {
				btn_get_code.setText(second-- + "s");
				btn_get_code.setEnabled(false);
			} else {
				btn_get_code.setText(R.string.register_get_code);
				btn_get_code.setEnabled(true);

				second = 60;

				Message message = new Message();
				message.what = 1;
				handlerStop.sendMessage(message);
			}

			handler.postDelayed(this, 1000);
		}
	};

	final Handler handlerStop = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				handler.removeCallbacks(runnable);
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_title_txt.setText("修改邮箱");
		home_title.setOnTouchListener(touch);
		email.addTextChangedListener(watcher);
		et_code.addTextChangedListener(watcher);

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	private TextWatcher watcher = new TextWatcher() {
		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (TextUtils.isEmpty(et_code.getText().toString())
					|| TextUtils.isEmpty(email.getText().toString()) || TextUtils.isEmpty(mCodeId)
					|| !RegPatterns.EmailPattern.matcher(email.getText().toString()).matches()) {
				save.setEnabled(false);
			} else {
				save.setEnabled(true);
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (handlerStop != null && handler != null && runnable != null) {
			Message message = new Message();
			message.what = 1;
			handlerStop.sendMessage(message);
		}
	}

}
